<?php

namespace App\Models\Common;

use App\Models\BaseModel;
use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class FsMessageTemplateModel extends BaseModel
{
    use HasFactory;

    protected $table = "common_fs_message_template";

    const MODULE_KMS       = "KMS";
    const MODULE_APP_CRASH = "APP_CRASH";

    /**
     * @param string $module
     * @param string $templateName
     * @return mixed
     * @throws Exception
     */
    public static function getMsgTemplate(string $module, string $templateName)
    {
        $msgTemplateModel = self::query()->where("module", $module)->where("templateName", $templateName)->first();
        if (empty($msgTemplateModel)) {
            throw new Exception("获取 {$module}-{$templateName} 消息模板失败");
        }
        if (empty(attributeGet($msgTemplateModel, "templateContent", ""))) {
            throw new Exception("消息模板 {$module}-{$templateName} 内容尚未配置");
        }
        return $msgTemplateModel->getAttribute("templateContent");
    }
}
